Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

Ext.onReady(function() {
	var tabs = new Ext.TabPanel({
				id : 'consoletabs',
				region : 'center', // a center region is ALWAYS
				// required for border layout
				closable : true,
				resizeTabs : true,
				minTabWidth : 135,
				tabWidth : 135,
				deferredRender : false,
				enableTabScroll : true,
				margins : '0 0 0 0',
				activeTab : 0, // first tab initially active
				items : []
			});

	// Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	var tree = new Ext.tree.TreePanel({
		region : 'center',
		useArrows : true,
		autoScroll : false,
		animate : false,
		enableDD : false,
		containerScroll : true,
		rootVisible : false,
		singleExpand : false,
		animate : true,
		border : false,
		frame : false,
		layout : 'fit',
		root : {
			nodeType : 'async'
		},
		// auto create TreeLoader
		dataUrl : context + '/system/console/menutree.json',
		listeners : {
			/*
			 * 'render' : function(tp) {
			 * tp.getSelectionModel().on('selectionchange', function(tree, node) {
			 * if (node && node.leaf) { //
			 * showMenuOnTab(node.text,node.attributes.url); showme(node.id,
			 * node.text, node.attributes.url); } else if (node && !node.leaf) {
			 * node.expand(true); } }) }
			 */
			'click' : function(node) {
				if (node && node.leaf) {
					if (node.attributes.openType
							&& node.attributes.openType == 'open') {
						showMenuOnOpen(node.id, node.text, node.attributes.func);
					} else {
						showMenuOnTab(node.id, node.text, node.attributes.func);
					}
				} else if (node && !node.leaf && !node.isExpanded()) {
					node.expand(true);
				} else if (node && !node.leaf && node.isExpanded()) {
					node.collapse(true);
				}
			}

		}
	});

	var viewport = new Ext.Viewport({
		layout : 'border',
		items : [
				// create instance immediately
				new Ext.BoxComponent({
							region : 'north',
							cls : 'docs-header',
							el : 'header',
							border : false,
							margins : '0 0 5 0'
						}), {
					region : 'west',
					id : 'west-panel', // see Ext.getCmp() below
					title : '控制台工具栏',
					split : true,
					width : 210,
					maxSize : 400,
					collapsible : true,
					autoScroll : true,
					autoHeight : false,
					// autoWidth: true,
					margins : '0 0 0 5',
					tbar : [' ', {
								iconCls : 'icon-expand-all',
								tooltip : '全部展开',
								handler : function() {
									tree.root.expand(true);
								},
								text : "展开",
								scope : this
							}, '-', {
								iconCls : 'icon-collapse-all',
								tooltip : '全部收缩',
								handler : function() {
									tree.root.collapse(true);
								},
								text : "收缩",
								scope : this
							}, '-', {
								text : '功能菜单',
								menu : [{
											iconCls : 'param',
											tooltip : '系统核心配置参数',
											handler : function() {
												showSystemParam();
											},
											text : "系统参数",
											scope : this
										}, {
											iconCls : 'cmd',
											tooltip : '将在Groove基础上开发的模块菜单及系统角色信息导出未可用于初始化配置的xml文件',
											handler : function() {
												downloadSystemInit();
											},
											text : "系统配置",
											scope : this
										}, {
											iconCls : 'pwd',
											tooltip : '密码管理',
											handler : function() {
												editPWD();
											},
											text : "密码管理",
											scope : this
										},{
											iconCls : 'log',
											tooltip : '系统访问日志',
											handler : function() {
												initDesktopPortal();
											},
											text : "系统日志",
											scope : this
										}, {
											iconCls : 'icon-quit',
											tooltip : '退出控制台',
											handler : function() {
												Ext.MessageBox.confirm('确认',
														'确定要退出系统控制台吗？', logout);
											},
											text : "退出",
											scope : this
										}]
								// <-- Add the action directly to a menu
							}],
					items : [tree]
				}, tabs
		// in this instance the TabPanel is not wrapped by another panel
		// since no title is needed, this Panel is added directly
		// as a Container
		]
	});

	// tree.getNode().expand(true);
	tree.root.expand(true);

	initDesktopPortal('helptabs', 'desktopCenter');

	// tree.height = Ext.getCmp("west-panel").getHeight() + 80;

	// get a reference to the HTML element with id "hideit" and add a click
	// listener to it
	/*
	 * Ext.get("hideit").on('click', function() { // get a reference to the
	 * Panel that was created with id = // 'west-panel' var w =
	 * Ext.getCmp('west-panel'); // expand or collapse that Panel based on its
	 * collapsed property // state w.collapsed ? w.expand() : w.collapse(); });
	 */

	setTimeout(function() {
				Ext.get('loading').remove();
				Ext.get('loading-mask').fadeOut({
							remove : true
						});
			}, 250);
	var index = 2;

	/* 退出系统 */
	function logout(e) {
		if (e == "yes") {
			document.location = context + '/system/console?method=logout';
		}
	}
});

/* tab 页中显示 */
function showMenuOnTab(id, tlt, func) {
	var tabid = 'tab_' + id;
	
	if (!Ext.getCmp(tabid)) {
		eval('var tabObj = ' + func + '("'+tabid+'");');
		Ext.getCmp('consoletabs').add(tabObj);
	}
	
	Ext.getCmp('consoletabs').setActiveTab(tabid);
}

/* 弹出窗口显示 */
function showMenuOnOpen(id, tlt, func) {
	eval(func + '("' + id + '", "' + tlt + '");');
}

function showSystemParam() {
	var runtimeParamsForm = new Ext.FormPanel({
				id : 'runtimeParamsForm',
				labelWidth : 100, // label settings here
				// cascade unless
				frame : false,
				bodyStyle : 'border:0px; padding:5px;',
				border : false,
				width : 400,
				defaultType : 'textfield',
				defaults : {
					width : 250
				},
				items : [{
							fieldLabel : '系统版本号',
							readOnly: true,
							value : GROOVE_VERSION
						}, {
							fieldLabel : '访问IP',
							readOnly: true,
							value : XSYSTEM_CONSOLE_IP
						}, {
							fieldLabel : '字符集',
							readOnly: true,
							value : XSYSTEM_CHARSETNAME
						}, {
							fieldLabel : '会话过滤',
							readOnly: true,
							value : XSYSTEM_SESSIONFILTER
						}, {
							fieldLabel : '浏览器过滤',
							readOnly: true,
							value : XSYSTEM_CHECKBROWSER
						}, {
							fieldLabel : '数据源',
							readOnly: true,
							value : XSYSTEM_DATASOURCE
						}, {
							fieldLabel : 'SQL编制器',
							readOnly: true,
							xtype : 'textarea',
							height : 40,
							value : XSYSTEM_DBSQLKINTTER
						}, {
							fieldLabel : '大字段处理器',
							readOnly: true,
							xtype : 'textarea',
							height : 40,
							value : XSYSTEM_LOB_HANDLER
						}, {
							fieldLabel : '附件处理器',
							readOnly: true,
							xtype : 'textarea',
							height : 40,
							value : XSYSTEM_ANNEX_TRANSACTOR
						}]
			});

	var runtimeParamsWin = new Ext.Window({
				renderTo : Ext.getBody(),
				layout : 'fit',
				width : 400,
				height : 400,
				title : '系统运行期参数',
				resizable : false,
				plain : true,
				modal : true,

				items : [runtimeParamsForm],

				buttons : [{
							text : '关 闭',
							handler : function() {
								runtimeParamsWin.close();
								runtimeParamsWin = null;
							}
						}]
			});
	runtimeParamsWin.show(this);
}

/**
 * 下载系统初始化配置
 */
function downloadSystemInit(){
	document.getElementById('dldfrm').src = context + '/system/system.do?method=loadInitFile';
}